package ec.atreasure.com.core.fragments;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import ec.atreasure.com.core.activity.AtreasureActivity;
import me.yokeyword.fragmentation.SupportFragment;
import me.yokeyword.fragmentation.SupportFragmentDelegate;

/**
 * Description : fragment基类
 * Created by wangjin on 2019/6/22.
 * Job number：
 * Phone ：18301070822
 * Email： 120182051@qq.com
 * Person in charge : 汪渝栋
 * Leader：
 */
public abstract class BaseFragment extends SupportFragment {

    private View mRootView;

    /**
     * 设置界面布局
     *
     * @return
     */
    public abstract Object setLayout();

    /**
     * 视图绑定完成后续操作
     *
     * @param savedInstanceState
     * @param rootView
     */
    public abstract void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView);

    public <T extends View> T findViewById(@IdRes int viewId) {
        if (mRootView != null) {
            return mRootView.findViewById(viewId);
        }
        throw new NullPointerException("rootView is null");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        final View rootView;
        //资源id类型
        if (setLayout() instanceof Integer) {
            rootView = inflater.inflate((int) setLayout(), container, false);
        }
        //视图组件类型
        else if (setLayout() instanceof View) {
            rootView = (View) setLayout();
        } else {
            throw new ClassCastException("传入视图资源/视图类型错误!!");
        }

        this.mRootView = rootView;
        onBindView(savedInstanceState, rootView);
        return rootView;
    }

    public final AtreasureActivity getAtreasureActivity() {
        return (AtreasureActivity) getActivity();
    }
}
